package sgd.tp2.client.net.tcp;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.xml.bind.JAXBException;

import sgd.tp2.api.cdr.CDR;
import sgd.tp2.api.cdr.CDRHandlerJSON;
import sgd.tp2.api.net.CDRFormatType;

public class TCPClientJSON extends TCPClient {

	private DataOutputStream dataOutStream = null;

	public TCPClientJSON(CDRFormatType cdrFormatType, String serverAddress, int serverPort) {
		super(cdrFormatType, serverAddress, serverPort);
	}

	@Override
	public void startClient() throws UnknownHostException, IOException {
		super.startClient();
		dataOutStream = new DataOutputStream(socket.getOutputStream());
	}

	@Override
	public void sendMessage() throws IOException, JAXBException {
		CDR cdr = CDR.getWithRandomData();
		String json = CDRHandlerJSON.getJSONFromCDR(cdr);

		dataOutStream.writeUTF(json);
	}
}
